package com.camel.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.camel.base.model.TbDatasource;
import com.camel.base.model.ZsOption;
import com.camel.base.service.TbDatasourceService;
import com.camel.base.service.ZsOptionService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.BasePaginationEntity;
import com.camel.core.entity.IParams;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.PaginationUtil;
import com.camel.core.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

/**
 * @author camel
 */
@RestController
@RequestMapping("datasource")
public class TbDatasourceController extends BaseCommonController<TbDatasource> {

    @Autowired
    private TbDatasourceService<TbDatasource> service;


    /**
     * 获取所有模式
     * @param id
     * @return
     */
    @PostMapping("/model/{id}")
    public Result model(@PathVariable Integer id, @RequestBody IParams params) throws SQLException {
        TbDatasource datasource = service.getById(id);
        List<JSONObject> model = service.model(datasource);
        PageInfo<JSONObject> objectPageInfo = new PageInfo<>();
        int start = (params.getPage()- 1) * params.getPerPage();
        int end = params.getPage() * params.getPerPage();

        int i = model.size() / params.getPerPage() + 1;
        if(start > i * params.getPerPage()) {
            start = i * params.getPerPage();
        }
        if(end > model.size()) {
            end = model.size();
        }


        objectPageInfo.setList(model.subList(start, end));
        objectPageInfo.setTotal(model.size());
        objectPageInfo.setPageNum(params.getPage());
        objectPageInfo.setSize(params.getPerPage());
        int[] ints = new int[i];
        for (int j = 0; j < i; j++) {
            ints[j] = j+1;
        }
        objectPageInfo.setNavigatepageNums(ints);
        objectPageInfo.setNavigateFirstPage(1);
        objectPageInfo.setNavigateLastPage(i);
        objectPageInfo.setPageSize(params.getPerPage());
        return ResultUtil.success(objectPageInfo);
    }

    @PostMapping("/table/{id}/{schama}")
    public Result tables(@PathVariable Integer id, @PathVariable String schama, @RequestBody IParams params) throws SQLException {
        TbDatasource datasource = service.getById(id);
        List<JSONObject> tables = service.table(datasource, schama, params.getParams());

        int start = (params.getPage()- 1) * params.getPerPage();
        int end = params.getPage() * params.getPerPage();
        int i = tables.size() / params.getPerPage() + 1;
        if(start > i * params.getPerPage()) {
            start = i * params.getPerPage();
        }
        if(end > tables.size()) {
            end = tables.size();
        }
        int[] ints = new int[i];
        for (int j = 0; j < i; j++) {
            ints[j] = j+1;
        }
        PageInfo<JSONObject> objectPageInfo = new PageInfo<>();
        objectPageInfo.setList(tables.subList(start, end));
        objectPageInfo.setTotal(tables.size());
        objectPageInfo.setPageNum(params.getPage());
        objectPageInfo.setSize(params.getPerPage());
        objectPageInfo.setNavigatepageNums(ints);
        objectPageInfo.setNavigateFirstPage(1);
        objectPageInfo.setNavigateLastPage(i);
        objectPageInfo.setPageSize(params.getPerPage());
        return ResultUtil.success(objectPageInfo);
    }


    /**
     * 查询所有字段
     * @param params
     * @return
     * @throws SQLException
     */
    @PostMapping("/column")
    public Result column(@RequestBody JSONObject params) throws SQLException {
        Integer id = params.getInteger("id");
        TbDatasource datasource = service.getById(id);
        List<Object> columns = service.columns(datasource, params.getString("schama"), params.getString("tableName"));
        return ResultUtil.success(columns);
    }



    @Override
    public boolean deleteAble(Serializable id) {
        TbDatasource byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    @Override
    public BaseCommonService<TbDatasource> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "数据源";
    }
}
